package com.piece.mall.controller;

import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.mall.model.PmsAttrGroup;
import com.piece.mall.service.PmsAttrGroupRelationService;
import com.piece.mall.service.PmsAttrGroupService;
import com.piece.mall.service.PmsCategoryService;
import com.piece.mall.service.PmsProductAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = {"商品属性分组管理"})
@RequestMapping("/api/mall/attr/group")
public class PmsAttrGroupController extends FrameWorkController {

    @Resource
    private PmsAttrGroupService pmsAttrGroupService;

    @Resource
    private PmsCategoryService pmsCategoryService;

    @Resource
    private PmsAttrGroupRelationService pmsAttrGroupRelationService;

    @Resource
    private PmsProductAttrValueService pmsProductAttrValueService;

    @PostMapping("/list")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "查询属性分组列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = pmsAttrGroupService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/table")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "查询属性分组分页数据")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public AjaxResponse table() {
        QueryTable queryTable = initTable();
        DataTable dataTable = pmsAttrGroupService.findPage(queryTable);
        return success(dataTable);
    }

    @GetMapping("/load")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "加载属性分组表单数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId", value = "主键"),
            @ApiImplicitParam(name="catalogId", value = "分类ID")
    })
    public AjaxResponse load() {
        long groupId = Convert.toLong(getRequest().getParameter("groupId"), 0L);
        long catalogId = Convert.toLong(getRequest().getParameter("catalogId"), 0L);
        PmsAttrGroup pmsAttrGroup = new PmsAttrGroup();
        if (groupId != 0) {
            pmsAttrGroup = pmsAttrGroupService.findById(groupId);
        } else {
            pmsAttrGroup.setCatalogId(catalogId);
            pmsAttrGroup.setGroupSort(pmsAttrGroupService.findNextGroupSort());
        }
        //设置分类路径
        pmsAttrGroup.setCatalogPath(pmsCategoryService.findPathById(pmsAttrGroup.getCatalogId()));

        return success(pmsAttrGroup);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "保存属性分组表单数据")
    @ApiImplicitParam(value = "属性分组数据", dataTypeClass = PmsAttrGroup.class, type = "body")
    public AjaxResponse save() {
        PmsAttrGroup pmsAttrGroup;
        try {
            Long attrGroupId = Convert.toLong(getRequest().getParameter("attrGroupId"));
            if (null != attrGroupId) {
                pmsAttrGroup = pmsAttrGroupService.findById(attrGroupId);
            } else {
                pmsAttrGroup = new PmsAttrGroup();
            }
            initDB(pmsAttrGroup);
            pmsAttrGroupService.insertOrUpdate(pmsAttrGroup);
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "删除属性分组数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        pmsAttrGroupService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "删除属性分组数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            pmsAttrGroupService.delete(Convert.toLongArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @PostMapping("/{groupId}/relation/table")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "查询当前属性分组关联的属性分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组主键", type = "path"),
            @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    })
    public AjaxResponse relationTable(@PathVariable Long groupId) {
        QueryTable queryTable = initTable();
        DataTable dataTable = pmsAttrGroupService.findRelationPage(queryTable, groupId);
        return success(dataTable);
    }

    @PostMapping("/{groupId}/unRelation/table")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "查询当前属性分组未关联的属性分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组主键", type = "path"),
            @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    })
    public AjaxResponse unRelationTable(@PathVariable Long groupId) {
        QueryTable queryTable = initTable();
        DataTable dataTable = pmsAttrGroupService.findUnRelationPage(queryTable, groupId);
        return success(dataTable);
    }

    @SubmitPermission
    @PostMapping("/{groupId}/relation/save")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "添加属性分组关联")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组主键", type = "path"),
            @ApiImplicitParam(name = "attrIds", value="属性主键集合")
    })
    public AjaxResponse saveRelation(@PathVariable Long groupId) throws Exception {
        String attrIds = getRequest().getParameter("attrIds");
        if (StringUtil.isNotEmpty(attrIds)) {
            pmsAttrGroupRelationService.saveRelation(groupId, Convert.toLongArray(attrIds));
        }
        return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/{groupId}/relation/delete")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "删除属性分组关联")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "分组主键", type = "path"),
            @ApiImplicitParam(name = "attrIds", value="属性主键集合")
    })
    public AjaxResponse deleteRelation(@PathVariable Long groupId) throws Exception {
        String attrIds = getRequest().getParameter("attrIds");
        if (StringUtil.isNotEmpty(attrIds)) {
            pmsAttrGroupRelationService.deleteRelationByGroupId(groupId, Convert.toLongArray(attrIds));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @PostMapping("/attr_value/list")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "查询spu属性分组值列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List attrList() {
        QueryTable queryTable = initTable();
        DataTable dataTable = pmsProductAttrValueService.findPage(queryTable);
        return dataTable.getData();
    }

    @PostMapping("/{catalogId}/detail/list")
    @AclPermission(code = "mall:product:attr")
    @ApiOperation(value = "查询属性分组详情展示列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "catalogId", value = "分类主键", type = "path"),
            @ApiImplicitParam(name = "attrType", value="属性类型")
    })
    public List attrList(@PathVariable Long catalogId) {
        Integer attrType = Convert.toInt(getRequest().getParameter("attrType"));
        return pmsAttrGroupService.getAttrGroupWithAttrs(catalogId, attrType);
    }
}
